package ControlLayer;
import ModelLayer.*;
import java.util.*;

/**
 * Write a description of class LoanCtr here.
 * 
 * @author (DM80 Group 5, Anders, Michael, Jesper, Lars og Kenni) 
 * @version (16/11-2012)
 */
public class LoanCtr
{
    private AddressCtr addressCtr;
    private DVDCtr dvdCtr;
    private LoanLibrary loanLib;

    public LoanCtr()
    {
        addressCtr = new AddressCtr();
        dvdCtr = new DVDCtr();
        loanLib = LoanLibrary.getLoanLibrary();
    }

    public boolean createLoan(int _loanId, String _loanDate, String _loanPeriod, String _loanStatus, int _personId, int _dvdId, int _sampleSerialNr)
    {
        boolean success = false;
        Person _person = addressCtr.findPerson(_personId);
        
        Sample _sample = dvdCtr.getSampleFromDvdById(_dvdId, _sampleSerialNr);
        
        if(_person != null && _sample != null)
        {
            Loan newLoan = new Loan(_loanId, _loanDate, _loanPeriod, _loanStatus, _person, _sample);

            loanLib.addLoan(newLoan);
            success = true;
        }
        
        return success;
    }
    
    public boolean returnDvd(int _id)
    {
        boolean success = false;
        
        Loan deleteLoan = loanLib.searchLoanById(_id);
        
        if(deleteLoan != null)
        {
            loanLib.deleteLoan(deleteLoan);
            success = true;
        }
        
        return success;  
    }
    
    public String listAllLoans()
    {
        String retStr = "";
        ArrayList<Loan> lstLoans = loanLib.getAllLoans();
        
        if(lstLoans != null)
        {
            retStr += "List Loans:" + "\n";
            
            for(Loan loan : lstLoans)
            {
                retStr += "Name: " + loan.getPerson().getName() + " Sample Nr: " + loan.getSample().getSerialNr() + "\n";
            }
        }
        
        return retStr;
    }
    
    public String getPersonInfo(int _id)
    {
        return addressCtr.getPerson(_id);
    }
    
    public String getDvdInfo(int _id)
    {
        return dvdCtr.findDVD(_id);
    }
}
